package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 全排列 II
 * @author qulq
 * @date 2024-07-08 21:58:45
 */
public class T47_PermutationsIi{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T47_PermutationsIi().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<Integer>> ans = new ArrayList<>();
	List<Integer> path = new ArrayList<>();
	boolean[] used;

    public List<List<Integer>> permuteUnique(int[] nums) {
		Arrays.sort(nums);
		used = new boolean[nums.length];
		backtracking(nums);
		return ans;
    }

	private void backtracking(int[] nums) {
		if (path.size() == nums.length) {
			ans.add(new ArrayList<>(path));
			return;
		}
		for (int i = 0; i < nums.length; i++) {
			if (i > 0 && nums[i - 1] == nums[i] && used[i - 1] == false) {
				continue;
			}
			if (used[i] == true) {
				continue;
			}
			path.add(nums[i]);
			used[i] = true;
			backtracking(nums);
			path.remove(path.size() - 1);
			used[i] = false;
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}